package com.campus.dao.impl;

import com.campus.dao.ClassDao;
import com.campus.model.Class;
import com.mysql.cj.util.DnsSrv;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.List;

@Repository
@Slf4j
public class ClassDaoImpl implements ClassDao {
    @Autowired
    Connection connection;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public Integer addClass(Class camp_class) {
        //判断活动是否发布
        String checkActivitySql = "SELECT COUNT(*) FROM t_manager_activity WHERE id = ? AND status = 1";
        Integer activityCount = jdbcTemplate.queryForObject(checkActivitySql, new Object[]{camp_class.getActivity_id()}, Integer.class);
        if (activityCount == null || activityCount == 0) {
            throw new IllegalArgumentException("Activity must be published (status = 1).");
        }

        String sql = "INSERT INTO t_manager_class (name, created_time, class_desc, activity_id) VALUES (?, ?, ?, ?)";
        Object[] args = new Object[]{
                camp_class.getName(),
                camp_class.getCreated_time(),
                camp_class.getClass_desc(),
                //camp_class.getTeacher_id(),
                camp_class.getActivity_id(),
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer modifyClass(Class newclass) {
        //判断活动是否发布
        String checkActivitySql = "SELECT COUNT(*) FROM t_manager_activity WHERE id = ? AND status = 1";
        Integer activityCount = jdbcTemplate.queryForObject(checkActivitySql, new Object[]{newclass.getActivity_id()}, Integer.class);
        if (activityCount == null || activityCount == 0) {
            throw new IllegalArgumentException("Activity must be published (status = 1).");
        }
        String sql = " UPDATE t_manager_class SET name=?,class_desc=?,teacher_id=?,activity_id=? WHERE id=?";
        Object[] args = new Object[]{
                newclass.getName(),
                newclass.getClass_desc(),
                newclass.getTeacher_id(),
                newclass.getActivity_id(),
                newclass.getClass_id(),
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer allocateTeacher(int class_id,int teacher_id) {
        String sql = " UPDATE t_manager_class SET teacher_id=? WHERE id=?";
        return jdbcTemplate.update(sql,new Object[]{teacher_id,class_id});
    }

    @Override
    public Integer addStudentsToClass(int classId, List<Integer> studentIds) {
        int rowsInserted = 0;
        String sql = "INSERT INTO t_teaching_student_in_class (class_id, student_id) VALUES (?, ?)";

        try {
            for (Integer studentId : studentIds) {
                rowsInserted += jdbcTemplate.update(sql, classId, studentId);
            }
            System.out.println("Students added to class successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rowsInserted;
    }
}
